//计算每年的均值

Map.centerObject(roi, 8);
for (var i = 1; i < 12; i++) {
    var year = '2008-'
    var start = year + i + '-01';
    var end = year + (i + 1) + '-01';
    var dataset = ee.ImageCollection('MODIS/006/MOD13Q1')
        .filter(ee.Filter.date(start, end))
        .map(function (image) {
            var midimage = image.clip(roi)
            return midimage.addBands(midimage.normalizedDifference(['sur_refl_b02', 'sur_refl_b01']).rename("myNDVI"))
        });

    var ndvi = dataset.select('myNDVI').mean();
    print(i + '月', ndvi.reduceRegion({
        reducer: ee.Reducer.mean(),
        geometry: roi,
        maxPixels: 1e13,
        scale: 30
    }))




}
//
//计算每年的均值后进行线性分析

Map.centerObject(roi, 8);
var imageList = [];
for (var j = 2005; j < 2020; j++) {
    var start = j + '-7-01';
    var end = j + '-10-01';
    var dataset = ee.ImageCollection('MODIS/006/MOD13Q1')
        .filter(ee.Filter.date(start, end))
        .map(function (image) {
            var midimage = image.clip(roi)
            return midimage.addBands(midimage.normalizedDifference(['sur_refl_b02', 'sur_refl_b01']).rename("myNDVI"))
        })
        .map(function (image) {
            return ee.Image(j)
                .float()
                .addBands(image);
        })
        .mean()
    imageList.push(dataset)
}
var allimage = ee.ImageCollection(imageList)
print(allimage)
var linearFit = allimage.select(['constant', 'myNDVI'])
    .reduce(ee.Reducer.linearFit());
print(linearFit)
print(2 + '月', linearFit.select('scale').reduceRegion({
    reducer: ee.Reducer.mean(),
    geometry: roi,
    maxPixels: 1e13,
    scale: 30
}))
Map.addLayer(linearFit, {
    min: 0,
    max: [-0.9, 8e-5, 1],
    bands: ['scale', 'offset', 'scale']
}, 'fit');

Export.image.toDrive({
    image: linearFit.select('scale'),
    description: 'imagescale',
    folder: "lunwen",
    scale: 30,
    region: roi,
    crs: "EPSG:4326",
    maxPixels: 1e13
});

//

Map.centerObject(roi, 8);
for (var i = 1; i < 13; i++) {
    var monthyearList = [];
    for (var j = 2005; j < 2020; j++) {
        var year = j + '-'
        var start = year + i + '-01';
        var end = year + (i) + '-30';
        if (i === 2) {
            end = year + (i) + '-28'
        }
        var dataset = ee.ImageCollection('MODIS/006/MOD13Q1')
            .filter(ee.Filter.date(start, end))
            .map(function (image) {
                var midimage = image.clip(roi)
                return midimage.addBands(midimage.normalizedDifference(['sur_refl_b02', 'sur_refl_b01']).rename("myNDVI"))
            });

        var ndvi = dataset.select('myNDVI').mean();
        monthyearList.push(ndvi)
    }
    var montheve = ee.ImageCollection(monthyearList).mean()
    print(i + '月', montheve.reduceRegion({
        reducer: ee.Reducer.mean(),
        geometry: roi,
        maxPixels: 1e13,
        scale: 30
    }))

}
//得到每一年的每个月的ndvi
Map.centerObject(roi, 8);
for (var j = 2005; j < 2020; j++) {
    print('-----' + j + '------')
    for (var i = 1; i < 13; i++) {
        var year = j + '-'
        var start = year + i + '-01';
        var end = year + (i) + '-30';
        if (i === 2) {
            end = year + (i) + '-28'
        }
        var dataset = ee.ImageCollection('MODIS/006/MOD13Q1')
            .filter(ee.Filter.date(start, end))
            .map(function (image) {
                var midimage = image.clip(roi)
                return midimage.addBands(midimage.normalizedDifference(['sur_refl_b02', 'sur_refl_b01']).rename("myNDVI"))
            });

        var ndvi = dataset.select('myNDVI').mean();
        print(i + '月', ndvi.reduceRegion({
            reducer: ee.Reducer.mean(),
            geometry: roi,
            maxPixels: 1e13,
            scale: 30
        }))

    }

}